package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Exame;

import com.model.Historico;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table HISTORICO.
*/
public class HistoricoDao extends AbstractDao<Historico, Long> {

    public static final String TABLENAME = "HISTORICO";

    /**
     * Properties of entity Historico.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_idHistorico");
        public final static Property Data = new Property(1, java.util.Date.class, "data", false, "DATA");
        public final static Property Colmeia = new Property(2, String.class, "colmeia", false, "COLMEIA");
        public final static Property Tipocaixa = new Property(3, String.class, "tipocaixa", false, "TIPOCAIXA");
        public final static Property Grupo = new Property(4, String.class, "grupo", false, "GRUPO");
        public final static Property Tipoextrado = new Property(5, String.class, "tipoextrado", false, "TIPOEXTRADO");
        public final static Property Niveldefesa = new Property(6, String.class, "niveldefesa", false, "NIVELDEFESA");
        public final static Property Produtividade = new Property(7, String.class, "produtividade", false, "PRODUTIVIDADE");
        public final static Property Nivelreserva = new Property(8, String.class, "Nivelreserva", false, "NIVELRESERVA");
        public final static Property QtdAlimenta = new Property(9, Integer.class, "qtdAlimenta", false, "QTD_ALIMENTA");
        public final static Property Possuiredutoralvado = new Property(10, Boolean.class, "possuiredutoralvado", false, "POSSUIREDUTORALVADO");
        public final static Property Possiutampoecluidor = new Property(11, Boolean.class, "possiutampoecluidor", false, "POSSIUTAMPOECLUIDOR");
        public final static Property Possiutabdetecvaroa = new Property(12, Boolean.class, "possiutabdetecvaroa", false, "POSSIUTABDETECVAROA");
        public final static Property Possiucaptpolen = new Property(13, Boolean.class, "possiucaptpolen", false, "POSSIUCAPTPOLEN");
        public final static Property QtdAlsa = new Property(14, Integer.class, "qtdAlsa", false, "QTD_ALSA");
        public final static Property Niveilreserva = new Property(15, String.class, "Niveilreserva", false, "NIVEILRESERVA");
        public final static Property Probenxemacao = new Property(16, Boolean.class, "probenxemacao", false, "PROBENXEMACAO");
        public final static Property ProdMel = new Property(17, String.class, "prodMel", false, "PROD_MEL");
        public final static Property ProdPropolis = new Property(18, String.class, "prodPropolis", false, "PROD_PROPOLIS");
        public final static Property ProdPolen = new Property(19, String.class, "prodPolen", false, "PROD_POLEN");
        public final static Property CodColmeia = new Property(20, long.class, "codColmeia", false, "COD_COLMEIA");
    };

    private DaoSession daoSession;

    private Query<Historico> exame_ExamecodColmeiaQuery;

    public HistoricoDao(DaoConfig config) {
        super(config);
    }
    
    public HistoricoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'HISTORICO' (" + //
                "'_idHistorico' INTEGER PRIMARY KEY ASC AUTOINCREMENT ," + // 0: id
                "'DATA' INTEGER," + // 1: data
                "'COLMEIA' TEXT," + // 2: colmeia
                "'TIPOCAIXA' TEXT," + // 3: tipocaixa
                "'GRUPO' TEXT," + // 4: grupo
                "'TIPOEXTRADO' TEXT," + // 5: tipoextrado
                "'NIVELDEFESA' TEXT," + // 6: niveldefesa
                "'PRODUTIVIDADE' TEXT," + // 7: produtividade
                "'NIVELRESERVA' TEXT," + // 8: Nivelreserva
                "'QTD_ALIMENTA' INTEGER," + // 9: qtdAlimenta
                "'POSSUIREDUTORALVADO' INTEGER," + // 10: possuiredutoralvado
                "'POSSIUTAMPOECLUIDOR' INTEGER," + // 11: possiutampoecluidor
                "'POSSIUTABDETECVAROA' INTEGER," + // 12: possiutabdetecvaroa
                "'POSSIUCAPTPOLEN' INTEGER," + // 13: possiucaptpolen
                "'QTD_ALSA' INTEGER," + // 14: qtdAlsa
                "'NIVEILRESERVA' TEXT," + // 15: Niveilreserva
                "'PROBENXEMACAO' INTEGER," + // 16: probenxemacao
                "'PROD_MEL' TEXT," + // 17: prodMel
                "'PROD_PROPOLIS' TEXT," + // 18: prodPropolis
                "'PROD_POLEN' TEXT," + // 19: prodPolen
                "'COD_COLMEIA' INTEGER NOT NULL );"); // 20: codColmeia
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'HISTORICO'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Historico entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        java.util.Date data = entity.getData();
        if (data != null) {
            stmt.bindLong(2, data.getTime());
        }
 
        String colmeia = entity.getColmeia();
        if (colmeia != null) {
            stmt.bindString(3, colmeia);
        }
 
        String tipocaixa = entity.getTipocaixa();
        if (tipocaixa != null) {
            stmt.bindString(4, tipocaixa);
        }
 
        String grupo = entity.getGrupo();
        if (grupo != null) {
            stmt.bindString(5, grupo);
        }
 
        String tipoextrado = entity.getTipoextrado();
        if (tipoextrado != null) {
            stmt.bindString(6, tipoextrado);
        }
 
        String niveldefesa = entity.getNiveldefesa();
        if (niveldefesa != null) {
            stmt.bindString(7, niveldefesa);
        }
 
        String produtividade = entity.getProdutividade();
        if (produtividade != null) {
            stmt.bindString(8, produtividade);
        }
 
        String Nivelreserva = entity.getNivelreserva();
        if (Nivelreserva != null) {
            stmt.bindString(9, Nivelreserva);
        }
 
        Integer qtdAlimenta = entity.getQtdAlimenta();
        if (qtdAlimenta != null) {
            stmt.bindLong(10, qtdAlimenta);
        }
 
        Boolean possuiredutoralvado = entity.getPossuiredutoralvado();
        if (possuiredutoralvado != null) {
            stmt.bindLong(11, possuiredutoralvado ? 1l: 0l);
        }
 
        Boolean possiutampoecluidor = entity.getPossiutampoecluidor();
        if (possiutampoecluidor != null) {
            stmt.bindLong(12, possiutampoecluidor ? 1l: 0l);
        }
 
        Boolean possiutabdetecvaroa = entity.getPossiutabdetecvaroa();
        if (possiutabdetecvaroa != null) {
            stmt.bindLong(13, possiutabdetecvaroa ? 1l: 0l);
        }
 
        Boolean possiucaptpolen = entity.getPossiucaptpolen();
        if (possiucaptpolen != null) {
            stmt.bindLong(14, possiucaptpolen ? 1l: 0l);
        }
 
        Integer qtdAlsa = entity.getQtdAlsa();
        if (qtdAlsa != null) {
            stmt.bindLong(15, qtdAlsa);
        }
 
        String Niveilreserva = entity.getNiveilreserva();
        if (Niveilreserva != null) {
            stmt.bindString(16, Niveilreserva);
        }
 
        Boolean probenxemacao = entity.getProbenxemacao();
        if (probenxemacao != null) {
            stmt.bindLong(17, probenxemacao ? 1l: 0l);
        }
 
        String prodMel = entity.getProdMel();
        if (prodMel != null) {
            stmt.bindString(18, prodMel);
        }
 
        String prodPropolis = entity.getProdPropolis();
        if (prodPropolis != null) {
            stmt.bindString(19, prodPropolis);
        }
 
        String prodPolen = entity.getProdPolen();
        if (prodPolen != null) {
            stmt.bindString(20, prodPolen);
        }
        stmt.bindLong(21, entity.getCodColmeia());
    }

    @Override
    protected void attachEntity(Historico entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Historico readEntity(Cursor cursor, int offset) {
        Historico entity = new Historico( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : new java.util.Date(cursor.getLong(offset + 1)), // data
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // colmeia
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // tipocaixa
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // grupo
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // tipoextrado
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // niveldefesa
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // produtividade
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // Nivelreserva
            cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9), // qtdAlimenta
            cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0, // possuiredutoralvado
            cursor.isNull(offset + 11) ? null : cursor.getShort(offset + 11) != 0, // possiutampoecluidor
            cursor.isNull(offset + 12) ? null : cursor.getShort(offset + 12) != 0, // possiutabdetecvaroa
            cursor.isNull(offset + 13) ? null : cursor.getShort(offset + 13) != 0, // possiucaptpolen
            cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14), // qtdAlsa
            cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15), // Niveilreserva
            cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0, // probenxemacao
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // prodMel
            cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18), // prodPropolis
            cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19), // prodPolen
            cursor.getLong(offset + 20) // codColmeia
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Historico entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setData(cursor.isNull(offset + 1) ? null : new java.util.Date(cursor.getLong(offset + 1)));
        entity.setColmeia(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setTipocaixa(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setGrupo(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setTipoextrado(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setNiveldefesa(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setProdutividade(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setNivelreserva(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setQtdAlimenta(cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9));
        entity.setPossuiredutoralvado(cursor.isNull(offset + 10) ? null : cursor.getShort(offset + 10) != 0);
        entity.setPossiutampoecluidor(cursor.isNull(offset + 11) ? null : cursor.getShort(offset + 11) != 0);
        entity.setPossiutabdetecvaroa(cursor.isNull(offset + 12) ? null : cursor.getShort(offset + 12) != 0);
        entity.setPossiucaptpolen(cursor.isNull(offset + 13) ? null : cursor.getShort(offset + 13) != 0);
        entity.setQtdAlsa(cursor.isNull(offset + 14) ? null : cursor.getInt(offset + 14));
        entity.setNiveilreserva(cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15));
        entity.setProbenxemacao(cursor.isNull(offset + 16) ? null : cursor.getShort(offset + 16) != 0);
        entity.setProdMel(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setProdPropolis(cursor.isNull(offset + 18) ? null : cursor.getString(offset + 18));
        entity.setProdPolen(cursor.isNull(offset + 19) ? null : cursor.getString(offset + 19));
        entity.setCodColmeia(cursor.getLong(offset + 20));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Historico entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Historico entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "examecodColmeia" to-many relationship of Exame. */
    public List<Historico> _queryExame_ExamecodColmeia(long codColmeia) {
        synchronized (this) {
            if (exame_ExamecodColmeiaQuery == null) {
                QueryBuilder<Historico> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.CodColmeia.eq(null));
                exame_ExamecodColmeiaQuery = queryBuilder.build();
            }
        }
        Query<Historico> query = exame_ExamecodColmeiaQuery.forCurrentThread();
        query.setParameter(0, codColmeia);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getExameDao().getAllColumns());
            builder.append(" FROM HISTORICO T");
            builder.append(" LEFT JOIN EXAME T0 ON T.'COD_COLMEIA'=T0.'_idExame'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Historico loadCurrentDeep(Cursor cursor, boolean lock) {
        Historico entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }

        return entity;    
    }

    public Historico loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Historico> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Historico> list = new ArrayList<Historico>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Historico> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Historico> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
